package net.sky.oa.service.infopub.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import net.sky.oa.dao.infopub.InfNewsDao;
import net.sky.oa.model.InfNews;
import net.sky.oa.model.SysEmployee;
import net.sky.oa.service.infopub.InfNewsService;
import net.sky.oa.service.system.SysEmployeeService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.htsoft.core.service.impl.BaseServiceImpl;
import com.htsoft.core.web.paging.PagingBean;
import com.htsoft.oa.model.info.InMessage;
import com.htsoft.oa.model.info.ShortMessage;
import com.htsoft.oa.service.info.InMessageService;
import com.htsoft.oa.service.info.ShortMessageService;

@Service
@Transactional
public class InfNewsServiceImpl extends BaseServiceImpl<InfNews> implements
		InfNewsService {
	
	private InfNewsDao dao;
	
	@Resource
	private SysEmployeeService sysEmployeeService;
	
	@Resource
	private ShortMessageService shortMessageService;
	
	@Resource
	private InMessageService inMessageService; 
	
	@Resource
	public void setInfNewsDao(InfNewsDao dao) {
		super.setDao(dao);
		this.dao = dao;
	}
	
	public List<InfNews> getDisplay(PagingBean pb) {
		return this.dao.getDisplay(pb);
	}

	public void save(InfNews entity, boolean operType) {
		this.dao.save(entity);
		try {
			if(entity.getStatus()==1 && entity.getCreatetime() != null){
				 if (operType&&entity.getIsims() == 1) {
					 	
						List<SysEmployee> list = sysEmployeeService
								.getNewEmpl(null);
						ShortMessage message = new ShortMessage();
						message.setContent("温馨提示，发布了新的新闻【" + entity.getTitle()
								+ "】,请注意查收!");
						message.setMsgType(Short.valueOf("5"));
						message.setSenderId(entity.getUser().getId());
						message.setSender(entity.getUser().getName());
						message.setSendTime(new Date());
						this.shortMessageService.save(message);
						for (SysEmployee emp : list) {
							InMessage in = new InMessage();
							in.setDelFlag(Short.valueOf("0"));
							in.setReadFlag(Short.valueOf("0"));
							in.setShortMessage(message);
							in.setUserId(emp.getId());
							in.setUserFullname(emp.getName());
							this.inMessageService.save(in);
						}
					}
			}
		} catch (Exception e) {
			e.printStackTrace();
					
				}
	}
	
	
	
}
